//
//  PEChioceCountyViewController.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/14.
//

#import "RHChioceRegionTypeVC.h"
#import "RHWindowTipManage.h"

@interface RHChioceRegionTypeVC ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic ,strong) UIPickerView *pickerView;

@property (nonatomic ,copy) NSArray *array;

@property (nonatomic, copy) NSString *titleString;

@property (nonatomic, assign) NSInteger index;
@end

@implementation RHChioceRegionTypeVC

- (instancetype)initWithData:(NSArray *)dataArray{
    
    self = [super init];
    if (self) {
        self.array = dataArray;
    }
    return self;
}

- (instancetype)initWithTitle:(NSString*)title Data:(NSArray *)dataArray
{
    self = [super init];
    if (self) {
        self.array = dataArray;
        self.titleString = title;
    }
    return self;
}

- (NSArray *)array{
    if (!_array) {
        _array = [NSArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

-(void)setCurContent:(NSString *)curContent
{
    _curContent = curContent;
    kWeakSelf;
    [self.array enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([curContent isEqualToString:obj]) {
            weakSelf.index = idx;
            [weakSelf.pickerView reloadAllComponents];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.pickerView selectRow:weakSelf.index inComponent:0 animated:NO];
            });
            *stop = YES;
        }
    }];
}

- (void)setupUI{
    kWeakSelf;
    [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:touchView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:kCOLOR_HEX(@"#f5f5f5")];
    [self.view addSubview:bgView];
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:kCOLOR_HEX(@"#3DBB91") forState:UIControlStateNormal];
    [btn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [bgView addSubview:btn];

    CGFloat btnWidth = [btn.titleLabel.text getStringWidthFont:btn.titleLabel.font limitHeight:25] +15;
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(35);
        make.right.mas_offset(-15);
        make.width.mas_equalTo(btnWidth);
        make.centerY.mas_offset(0);
    }];
    
    CGFloat titleWidth = kUI_WINDOW_W - (btnWidth +30)*2;
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.numberOfLines = 0;
    titleLab.textColor = self.titleColor?self.titleColor:[UIColor blackColor];
    titleLab.font = [UIFont systemFontOfSize:16];
    titleLab.text = self.titleString?self.titleString:@"";
    [bgView addSubview:titleLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btn);
        make.width.mas_offset(titleWidth);
        make.right.equalTo(btn.mas_left).mas_offset(-15);
    }];
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kUI_WINDOW_H-(216+kTabbarSafeBottomMargin), kUI_WINDOW_W, 216+kTabbarSafeBottomMargin)];
    [_pickerView setDataSource:self];
    [_pickerView setDelegate:self];
    [_pickerView setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:_pickerView];
    
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(45);
        make.left.right.mas_offset(0);
        make.bottom.equalTo(_pickerView.mas_top);
    }];
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];

    
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [weakSelf hidden];
    }];
    

    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        __block BOOL isExit = NO;
        weakSelf.index = [weakSelf.pickerView selectedRowInComponent:0];
        [weakSelf.fitlerArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if([weakSelf.array[weakSelf.index] isEqualToString:obj]) {
                isExit = YES;
            }
        }];
//        if(isExit){
//            [QMUITips showError:@"此国家已存在国代".rh_localized];
//            return;
//        }
        
        if (weakSelf.selectBlock) {
            weakSelf.selectBlock(weakSelf.index);
        }
        [weakSelf hidden];
    }];
}

- (void)hidden{
   
    [[RHWindowTipManage shareWindowTipManage] hidden];
}


#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.array.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component API_UNAVAILABLE(tvos){
   
    return self.array[row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view API_UNAVAILABLE(tvos)
{
    
    UIView *bgView = kView;
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    [bgView addSubview:titleLab];
    titleLab.text = self.array[row];
    titleLab.textColor = [UIColor blackColor];
   
        if(row == self.index){
            titleLab.textColor = kCOLOR_HEX(@"#3DBB91");
        }
  
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_offset(0);
    }];
    return bgView;
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component API_UNAVAILABLE(tvos){
    self.index = row;
    [pickerView reloadAllComponents];
}



@end
